const util = require('../../utils/util.js')
Component({
  properties: {
    dataArr: {
      type: Object,
      value: () => { },
      observer: function (e) {
        let type, list, style;
        let {
          text,
          dataArr,
          stylesType
        } = e
        if (text == "限时秒杀") {
          type = 2
        } else if (text == "拼团商品") {
          type = 1
        }
        list = dataArr;
        style = stylesType;
        this.setData({
          type,
          list,
          style, // 0 是 不滚动的造型   -- 1 是 滚动的造型
        })
        var etime = e.dataArr[0].etime,
          stime = e.dataArr[0].stime;
        this.setData({
          etime,
          stime
        })
      }
    }
  },
  data: {
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    idx: 0,
    indexs: 0,
    status: -1,
    list: []
  },
  ready() {
    this.intervalTime()
  },
  methods: {
    intervalTime() {
      this.setData({
        timer: setInterval(() => {

          /*
 
           deviation === 传入时间和当前时间的差值
 
           如果  deviation >= 0   ------> 活动尚未开始
 
           如果  deviation <  0   ------> 活动已经开始
 
         */
          let {
            stime,
            etime,
            status
          } = this.data,
            nowTime = Math.floor(new Date() / 1000);
          /**
           *  deviation
           *  如果开始时间大于现在的时间  说明活动未开始 === 剩余时间是 开始时间减去 现在的时间
           *  如果开始时间小于现在的时间  说明活动已开始 === 剩余时间是 结束时间减去 现在的时间
           */
          // let deviation = stime - nowTime >= 0 ? stime - nowTime : nowTime - etime ;
          if (status == 1) {
            // 活动未开始
            deviation = stime - nowTime
          } else if (status == 2) {
            // 进行中
            deviation = nowTime - etime
          } else {
            // 活动寂静结束
            deviation = etime - nowTime
          }

          let {
            hrStr,
            minStr,
            secStr,
            day,
            shengSR
          } = this.getTimeminus(deviation >= 0 ? stime : etime)

          deviation = deviation - 1;
          // 如果活动未开始
          if (deviation >= 0) {
            this.setData({
              status: 1,
              isBegin: false,
              isFinish: false,
              shengSR,
              hrStr,
              minStr,
              secStr,
              day
            })
          } else {
            // 说明活动已经结束
            if (nowTime >= etime) {
              clearInterval(this.data.timer);
              this.setData({
                status: 3,
                shengSR: '00',
                hrStr: '00',
                minStr: '00',
                secStr: '00',
                day: '00',
              })
            } else {
              // 说明活动已经开始
              this.setData({
                status: 2,
                shengSR,
                hrStr,
                minStr,
                secStr,
                day
              })
            }
          }

        }, 1000)
      })
    },
    getTimeminus(current) {
      var deviation, day, hrStr, minStr, secStr, shengSR;
      currentTimeNum = Date.parse(new Date());
      // 传入时间和当前时间的差值
      deviation = current - currentTimeNum / 1000;
      //时间差(s)
      day = Math.floor(deviation / 3600 / 24);
      // 换算成天
      hr = Math.floor((deviation) / 3600);
      // 换算成小时
      shengSR = Math.floor(hr % 24);
      // 换算成剩余 小时
      min = Math.floor(((deviation) / 60) % 60);
      // 换算成剩余分
      sec = deviation % 60;
      // 换算成剩余秒

      hrStr = hr.toString();
      minStr = min.toString();
      secStr = sec.toString();
      shengSR = shengSR.toString();
      day = day.toString();
      if (minStr.length == 1) minStr = '0' + minStr;
      if (hrStr.length == 1) hrStr = '0' + hrStr;
      if (secStr.length == 1) secStr = '0' + secStr;
      if (shengSR.length == 1) shengSR = '0' + shengSR;
      if (day.length == 1) day = '0' + day;

      return {
        shengSR,
        hr,
        deviation,
        day,
        hrStr,
        minStr,
        secStr,
      }
    },
    choose(e) {
      let idx = e.currentTarget.dataset.idx
      if (idx != this.data.idx) {
        this.setData({
          idx,
          indexs: idx
        })
      }
    },
    checkTime() {
      let {
        etime,
        stime
      } = this.data
      if (etime - Date.now() / 1000 <= 0) {
        wx.showModal({
          title: '提示',
          content: '活动已过期',
          confirmText: '知道了',
        })
        return false
      }
      if (Date.now() / 1000 - stime <= 0) {
        wx.showModal({
          title: '提示',
          content: '活动未开始',
          confirmText: '知道了',
        })
        return false
      }
    },
    goPinTuan(e) {
      let canIUse = this.checkTime()
      if (canIUse == false) return false

      let goods_id = e.currentTarget.dataset.goods_id,
        limit_id = e.currentTarget.dataset.limit_id
      wx.navigateTo({
        url: `/pages/seckill/seckill?goods_id=${goods_id}&limit_id=${limit_id}`,
      })
    },
    seeMore(e) {
      let canIUse = this.checkTime()
      if (canIUse == false) return false

      let activity_id = e.currentTarget.dataset.activity_id
      wx.navigateTo({
        url: `/elsePage/pages/xianShiTeHui/xianShiTeHui?activity_id=${activity_id}&type=${this.data.type}&qtTitle=限时折扣&title=限时秒杀`,
      })
    }
  }
})